<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="订单编号" prop="orderSn">
      <el-input v-model="dataForm.orderSn" placeholder="订单编号"></el-input>
    </el-form-item>
    <el-form-item label="用户帐号" prop="username">
      <el-input v-model="dataForm.username" placeholder="用户帐号"></el-input>
    </el-form-item>
    <el-form-item label="订单总金额" prop="totalAmount">
      <el-input v-model="dataForm.totalAmount" placeholder="订单总金额"></el-input>
    </el-form-item>
    <el-form-item label="应付金额（实际支付金额）" prop="payAmount">
      <el-input v-model="dataForm.payAmount" placeholder="应付金额（实际支付金额）"></el-input>
    </el-form-item>
    <el-form-item label="运费金额" prop="freightAmount">
      <el-input v-model="dataForm.freightAmount" placeholder="运费金额"></el-input>
    </el-form-item>
    <el-form-item label="促销优化金额（促销价、满减、阶梯价）" prop="promotionAmount">
      <el-input v-model="dataForm.promotionAmount" placeholder="促销优化金额（促销价、满减、阶梯价）"></el-input>
    </el-form-item>
    <el-form-item label="积分抵扣金额" prop="integrationAmount">
      <el-input v-model="dataForm.integrationAmount" placeholder="积分抵扣金额"></el-input>
    </el-form-item>
    <el-form-item label="优惠券抵扣金额" prop="couponAmount">
      <el-input v-model="dataForm.couponAmount" placeholder="优惠券抵扣金额"></el-input>
    </el-form-item>
    <el-form-item label="管理员后台调整订单使用的折扣金额" prop="discountAmount">
      <el-input v-model="dataForm.discountAmount" placeholder="管理员后台调整订单使用的折扣金额"></el-input>
    </el-form-item>
    <el-form-item label="支付方式：1->未支付；2->支付宝；3->微信" prop="payType">
      <el-input v-model="dataForm.payType" placeholder="支付方式：1->未支付；2->支付宝；3->微信"></el-input>
    </el-form-item>
    <el-form-item label="订单来源：1->PC订单；2->app订单" prop="sourceType">
      <el-input v-model="dataForm.sourceType" placeholder="订单来源：1->PC订单；2->app订单"></el-input>
    </el-form-item>
    <el-form-item label="订单状态：1->待付款；2->待充值；3->充值中；4->充值成功；5->退款成功；-1->充值失败，待退款；-2->异常，重试中" prop="status">
      <el-input v-model="dataForm.status" placeholder="订单状态：1->待付款；2->待充值；3->充值中；4->充值成功；5->退款成功；-1->充值失败，待退款；-2->异常，重试中"></el-input>
    </el-form-item>
    <el-form-item label="订单充值类型：1->自动，2->手动" prop="rechangeType">
      <el-input v-model="dataForm.rechangeType" placeholder="订单充值类型：1->自动，2->手动"></el-input>
    </el-form-item>
    <el-form-item label="可以获得的积分" prop="integration">
      <el-input v-model="dataForm.integration" placeholder="可以获得的积分"></el-input>
    </el-form-item>
    <el-form-item label="下单时使用的积分" prop="useIntegration">
      <el-input v-model="dataForm.useIntegration" placeholder="下单时使用的积分"></el-input>
    </el-form-item>
    <el-form-item label="删除状态：0->未删除；1->已删除" prop="deleteStatus">
      <el-input v-model="dataForm.deleteStatus" placeholder="删除状态：0->未删除；1->已删除"></el-input>
    </el-form-item>
    <el-form-item label="提交时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="提交时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          orderSn: '',
          username: '',
          totalAmount: '',
          payAmount: '',
          freightAmount: '',
          promotionAmount: '',
          integrationAmount: '',
          couponAmount: '',
          discountAmount: '',
          payType: '',
          sourceType: '',
          status: '',
          rechangeType: '',
          integration: '',
          useIntegration: '',
          deleteStatus: '',
          createTime: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          orderSn: [
            { required: true, message: '订单编号不能为空', trigger: 'blur' }
          ],
          username: [
            { required: true, message: '用户帐号不能为空', trigger: 'blur' }
          ],
          totalAmount: [
            { required: true, message: '订单总金额不能为空', trigger: 'blur' }
          ],
          payAmount: [
            { required: true, message: '应付金额（实际支付金额）不能为空', trigger: 'blur' }
          ],
          freightAmount: [
            { required: true, message: '运费金额不能为空', trigger: 'blur' }
          ],
          promotionAmount: [
            { required: true, message: '促销优化金额（促销价、满减、阶梯价）不能为空', trigger: 'blur' }
          ],
          integrationAmount: [
            { required: true, message: '积分抵扣金额不能为空', trigger: 'blur' }
          ],
          couponAmount: [
            { required: true, message: '优惠券抵扣金额不能为空', trigger: 'blur' }
          ],
          discountAmount: [
            { required: true, message: '管理员后台调整订单使用的折扣金额不能为空', trigger: 'blur' }
          ],
          payType: [
            { required: true, message: '支付方式：1->未支付；2->支付宝；3->微信不能为空', trigger: 'blur' }
          ],
          sourceType: [
            { required: true, message: '订单来源：1->PC订单；2->app订单不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '订单状态：1->待付款；2->待充值；3->充值中；4->充值成功；5->退款成功；-1->充值失败，待退款；-2->异常，重试中不能为空', trigger: 'blur' }
          ],
          rechangeType: [
            { required: true, message: '订单充值类型：1->自动，2->手动不能为空', trigger: 'blur' }
          ],
          integration: [
            { required: true, message: '可以获得的积分不能为空', trigger: 'blur' }
          ],
          useIntegration: [
            { required: true, message: '下单时使用的积分不能为空', trigger: 'blur' }
          ],
          deleteStatus: [
            { required: true, message: '删除状态：0->未删除；1->已删除不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '提交时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/order/orderinfo/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.orderinfo.userId
                this.dataForm.orderSn = data.orderinfo.orderSn
                this.dataForm.username = data.orderinfo.username
                this.dataForm.totalAmount = data.orderinfo.totalAmount
                this.dataForm.payAmount = data.orderinfo.payAmount
                this.dataForm.freightAmount = data.orderinfo.freightAmount
                this.dataForm.promotionAmount = data.orderinfo.promotionAmount
                this.dataForm.integrationAmount = data.orderinfo.integrationAmount
                this.dataForm.couponAmount = data.orderinfo.couponAmount
                this.dataForm.discountAmount = data.orderinfo.discountAmount
                this.dataForm.payType = data.orderinfo.payType
                this.dataForm.sourceType = data.orderinfo.sourceType
                this.dataForm.status = data.orderinfo.status
                this.dataForm.rechangeType = data.orderinfo.rechangeType
                this.dataForm.integration = data.orderinfo.integration
                this.dataForm.useIntegration = data.orderinfo.useIntegration
                this.dataForm.deleteStatus = data.orderinfo.deleteStatus
                this.dataForm.createTime = data.orderinfo.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/order/orderinfo/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'orderSn': this.dataForm.orderSn,
                'username': this.dataForm.username,
                'totalAmount': this.dataForm.totalAmount,
                'payAmount': this.dataForm.payAmount,
                'freightAmount': this.dataForm.freightAmount,
                'promotionAmount': this.dataForm.promotionAmount,
                'integrationAmount': this.dataForm.integrationAmount,
                'couponAmount': this.dataForm.couponAmount,
                'discountAmount': this.dataForm.discountAmount,
                'payType': this.dataForm.payType,
                'sourceType': this.dataForm.sourceType,
                'status': this.dataForm.status,
                'rechangeType': this.dataForm.rechangeType,
                'integration': this.dataForm.integration,
                'useIntegration': this.dataForm.useIntegration,
                'deleteStatus': this.dataForm.deleteStatus,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
